import { _decorator, Component, Animation, Label, AudioSource } from 'cc';
import { EnEvent } from '../const/EnEvent';
import { GameAPI } from '../GameAPI';
import { EnGameState, GameData } from '../GameData';
const { ccclass, property } = _decorator;

@ccclass('TimeNodeAnimation')
export class TimeNodeAnimation extends Component {

    private anim: Animation;
    private timeLabel: Label;
    private time: number = 0;
    private audio: AudioSource;

    public playerUid: any;

    start() {
        this.anim = this.node.getComponent(Animation);
        this.anim.stop();
        GameAPI.RegGEvent(EnEvent.PlaybackStateEvent, this.PlaybackStateEvent.bind(this), this);
        GameAPI.RegGEvent(EnEvent.UpdateAudioSttaEvent, this.UpdateAudioSttaEvent.bind(this), this);
    }

    onEnable() {
        this.unscheduleAllCallbacks();
        this.UpdateChildActive(true);
        if (GameData.outCardTime < 0) {
            this.UpdateChildActive(false);
            return;
        }
        this.timeLabel = this.node.getChildByName("TimeLabel").getComponent(Label);
        this.time = GameData.outCardTime;
        this.timeLabel.string = this.time.toString();
        this.node.setRotation(0, 0, 0, 0);
        this.onExecuteTimer();
    }

    UpdateAudioSttaEvent() {
        if (this.node.parent.name != "InGameBtns" && this.node.parent.name != "startCardBtns" && this.node.parent.name != "CanNotGameBtns") {
            return;
        }
        if (this.audio == null) {
            this.audio = this.node.getComponent(AudioSource);
        }
        if (GameData.isPlayAudio) {
            this.audio.enabled = true;
            this.audio.play();
            this.audio.volume = 0.5;
            this.audio.loop = false;
        }
        else {
            this.audio.enabled = false;
            this.audio.stop();
        }
    }

    onDisable() {
        if (this.anim) {
            this.anim.stop();
        }
        if (this.audio != null) {
            this.audio.stop();
        }
        this.unscheduleAllCallbacks();
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    onExecuteTimer() {
        let myThis = this;
        if (this.audio != null) {
            this.audio.stop();
        }
        this.schedule(function () {
            if (GameData.gameState == EnGameState.Settlement) {
                myThis.time = 0;
                myThis.unscheduleAllCallbacks();
                return;
            }
            myThis.time -= 1;
            if (myThis.time == 5) {
                myThis.anim.play();
            }
            else if (myThis.time < 0) {
                myThis.time = 0;
                myThis.unscheduleAllCallbacks();
                myThis.UpdateChildActive(false);
            }
            myThis.timeLabel.string = myThis.time.toString();
            if (myThis.time <= 10) {
                myThis.UpdateAudioSttaEvent();
            }
        }.bind(this), 1);
    }

    UpdateChildActive(active) {
        // for (let i = 0; i < this.node.children.length; i++) {
        //     const childNode = this.node.children[i];
        //     if (childNode) {
        //         childNode.active = active;
        //     }
        // }
    }

    private PlaybackStateEvent(data) {
        this.unscheduleAllCallbacks();
        if (data.play) {
            this.onExecuteTimer();
        }
    }
}

